<?php
use yii\helpers\Url;
?>
<style type="text/css">
.demo{width:620px; margin:30px auto}
.demo p{line-height:32px}
.btn{position: relative;overflow: hidden;margin-right: 4px;display:inline-block;*display:inline;padding:4px 10px 4px;font-size:14px;line-height:18px;*line-height:20px;color:#fff;text-align:center;vertical-align:middle;cursor:pointer;background-color:#5bb75b;border:1px solid #cccccc;border-color:#e6e6e6 #e6e6e6 #bfbfbf;border-bottom-color:#b3b3b3;-webkit-border-radius:4px;-moz-border-radius:4px;border-radius:4px;}
.btn input {position: absolute;top: 0; right: 0;margin: 0;border: solid transparent;opacity: 0;filter:alpha(opacity=0); cursor: pointer;}
.progress { position:relative; margin-left:100px; margin-top:-24px; width:200px;padding: 1px; border-radius:3px; display:none}
.progress1 { position:relative; margin-left:100px; margin-top:-24px; width:200px;padding: 1px; border-radius:3px; display:none}
.bar {background-color: green; display:block; width:0%; height:20px; border-radius: 3px; }
.percent { position:absolute; height:20px; display:inline-block; top:3px; left:2%; color:#fff }
.percent1 { position:absolute; height:20px; display:inline-block; top:3px; left:2%; color:#fff }
.files{height:22px; line-height:22px; margin:10px 0}
.delimg{margin-left:20px; color:#090; cursor:pointer}
</style>
<?php echo $this->render('_centernav')?>
  <div class="you">
  <div id="showimg"><img src="http://files.bwphp.com/<?php echo $model->user_thumb?>" alt="" width='150' height='80'></div>
    <div class="progress">
    		<span class="bar"></span><span class="percent">0%</span >
		</div>
    <div style="margin-top:420px">
		<div class="demo">   
   		<div class="btn">
            <span>上传头像</span>
            <input id="fileupload" type="file" name="mypic">
        </div>
        
        <div class="files"></div>
        
   </div>
	</div>
  </div> 
   
</div>
<script type="text/javascript" src="/assets/jquery.min.js"></script>
<script type="text/javascript" src="/assets/jquery.form.js"></script>
<script type="text/javascript">
var $j = jQuery.noConflict(); 
$j(function () {
	var bar = $j('.bar');
	var percent = $j('.percent');
	var showimg = $j('#showimg');
	var progress = $j(".progress");
	var files = $j(".files");
	var btn = $j(".btn span");
	$j("#fileupload").wrap("<form id='myupload' action='http://files.bwphp.com/actionu.php' method='post' enctype='multipart/form-data'></form>");
    $j("#fileupload").change(function(){
		$j("#myupload").ajaxSubmit({
			dataType:  'json',
			beforeSend: function() {
        		showimg.empty();
				progress.show();
        		var percentVal = '0%';
        		bar.width(percentVal);
        		percent.html(percentVal);
				btn.html("上传中...");
    		},
    		uploadProgress: function(event, position, total, percentComplete) {
        		var percentVal = percentComplete + '%';
        		bar.width(percentVal);
        		percent.html(percentVal);
    		},
			success: function(data) {
				files.html("<b>"+data.name+"("+data.size+"k)</b> <span class='delimg' rel='"+data.path+"'>删除</span>");
				var img = "http://files.bwphp.com/"+data.path;
				showimg.html("<img src='"+img+"' width='150' height='80'>");
				btn.html("添加软件封面");
				$j('#image_name').val(data.path);
				var uid = "<?php echo $model->user_id;?>";
				$.ajax({
				   type: "POST",
				   url: "<?= Url::to(['center/savethumb'])?>",
				   data: "user_id="+uid+"&user_thumb="+data.path,
				   success: function(msg){
					 if(msg == 1){
							
					 }else if(msg == 2){
						alert('数据库修改失败')		
					 }
				   }
				});
			},
			error:function(xhr){
				btn.html("上传失败");
				bar.width('0')
				files.html(xhr.responseText);
			}
		});
	});
	
	$j(".delimg").live('click',function(){
		var pic = $(this).attr("rel");
		$.post("http://files.bwphp.com/actionu.php?act=delimg",{imagename:pic},function(msg){
			if(msg==1){
				files.html("删除成功.");
				showimg.empty();
				progress.hide();
				$j('#image_name').val('');
			}else{
				alert(msg);
			}
		});
	}); 
});

</script>